package com.foo.TemporalDataProvider;

import java.sql.Date;
import java.sql.Timestamp;

import javax.ejb.Stateless;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.foo.DAO.SnapshotTemporalDAO;
import com.foo.DataSender.DataSender;
import com.foo.Entity.HistoricalData;
import com.foo.Writer.WriterInterface;
import com.foo.DataProvider.*;

/**
 * Session Bean implementation class TemporalDataProvider
 */
@Stateless
public class TemporalDataProvider implements ProviderInterface{//TemporalDataProviderRemote, TemporalDataProviderLocal {

	int total =0 ;
	String temporalmessage = ""; 
	DataSender datasender = new DataSender();
	
    public TemporalDataProvider() 
    {
    	System.out.println("Temporal Data Provider created!");
    }

	@Override
	public void getData(String sorttype, String value, Timestamp timestamp, Date fromdate, Date todate)
	{
		System.out.println("Entering server function Snapshot Data with parameters " +sorttype);

		System.out.println("Server side: if condition :");
		// Example for using named query
		temporalmessage = ""; 
		ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml");
		SnapshotTemporalDAO forumDAO = (SnapshotTemporalDAO) context.getBean("temporalDAO");   		
		HistoricalData[] datalist=forumDAO.selectForum(sorttype, fromdate, todate);		
	
		if(datalist != null)
		{
		for(int k=0 ; k< datalist.length ; k++)
		{
			temporalmessage += datalist[k].getTicker_symbol()+","+datalist[k].getDeals_date()+","+ datalist[k].getOpen_value() +"," +datalist[k].getHigh_value() 
			+","+datalist[k].getLow_value()+ ","+datalist[k].getClose_value()+","+datalist[k].getVolume()+",";

		}
		}
		else
		{
			temporalmessage= "";
		}
	System.out.println("The message " +temporalmessage);
	datasender.sendMessage("Temporal",temporalmessage);
	}
}

